﻿using ImageMagick;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using YYFDebugTools.CommonHelper;

namespace YYFDebugTools.ViewModels
{
    public class ImageOptionsVM : BaseModel
    {
        public ImageOptionsVM()
        {

        }
        public ObservableCollection<InputImage> InputImages { get; set; } = [];
        public ObservableCollection<ImageOptionsSetVM> ImageOptionsSetVMs { get; set; } = [];

        public void DisDatas()
        {
            foreach (var item in InputImages)
            {
                item.DisDatas(ImageOptionsSetVMs);
            }
        }


    }
    public class InputImage : BaseModel
    {
        public InputImage(string imagePath)
        {
            ImagePath = imagePath;
            BindImage();
        }
        private string ImagePath { get; set; }

        public void BindImage()
        {
            using FileStream stream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad; // 确保不会锁定内存流
            bitmap.StreamSource = stream;
            bitmap.EndInit();
            bitmap.Freeze(); // 冻结对象以提高性能
            ImgSource = bitmap;
        }

        private ImageSource imagesource;

        public ImageSource ImgSource
        {
            get { return imagesource; }
            set { imagesource = value; OnPropertyChanged(); }
        }

        public void DisDatas(ObservableCollection<ImageOptionsSetVM> imageOptionsSetVMs)
        {
            MagickImage comp = new MagickImage(ImagePath);
            foreach (var item in imageOptionsSetVMs)
            {
                item.SelectFunc?.CalImages(comp);
            }


        }

    }
}
